﻿using BookManege.entity;
using BookManege.service;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BookManege.controller
{
    public partial class userManege : Form
    {
        private UserManegeService userManegeService;
        private int pageNum;
        private int pageSize;

        public userManege()
        {
            InitializeComponent();
            this.userManegeService = new UserManegeService();
            this.pageNum = 1;
            this.pageSize = 10;
        }

        private void userManege_Load(object sender, EventArgs e)
        {
            GetAllUser();
            //男女下拉框
            List<GenderOption> genderOptions = new()
            {
                new GenderOption(0, "男"),
                new GenderOption(1, "女")
            };
            DataGridViewComboBoxColumn dataGridViewComboBoxColumn = dataGridView1.Columns["sex"] as DataGridViewComboBoxColumn;
            dataGridViewComboBoxColumn.DataSource = genderOptions;
            dataGridViewComboBoxColumn.ValueMember = "Value";
            dataGridViewComboBoxColumn.DisplayMember = "sex";

            textBox4.Text = pageNum.ToString();

            if (pageNum == 1)
            {
                linkLabel1.Enabled = false;
            }
        }
        public void GetAllUser()
        {
            dataGridView1.DataSource = userManegeService.GetAllUser(pageNum, pageSize);

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            string temp = dataGridView1.Columns[e.ColumnIndex].Name;
            DataGridViewRow clickRow = dataGridView1.Rows[e.RowIndex];
            long userId = (long)clickRow.Cells["id"].Value;
            if (temp == "删除")
            {
                if (MessageBox.Show("确认要删除用户 [" + clickRow.Cells["user_name"].Value.ToString() + "] 吗", "确认", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    MessageBox.Show(userManegeService.DeleteUser(userId) > 0 ? "删除成功!" : "删除失败!");
                }
                GetAllUser();
            }
            else if (temp == "修改")
            {
                //Decimal转换为Double必须用Convert.ToDouble保持精度和安全转换
                User user = new User(userId, clickRow.Cells["avatar"].Value.ToString(), clickRow.Cells["user_name"].Value.ToString(), clickRow.Cells["password"].Value.ToString(), clickRow.Cells["nick_name"].Value.ToString(),
                    (int)clickRow.Cells["sex"].Value, (int)clickRow.Cells["age"].Value, (int)clickRow.Cells["phone"].Value, clickRow.Cells["email"].Value.ToString(), (int)clickRow.Cells["authority"].Value);
                MessageBox.Show(userManegeService.UpdateUser(user) > 0 ? "更新成功!" : "更新失败!");
                GetAllUser();
            }
            else if (temp == "password")
            {
                if (MessageBox.Show("确认要重置用户 [" + clickRow.Cells["user_name"].Value.ToString() + "] 的密码吗", "确认", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    if (userManegeService.ResetPassword(userId) != null)
                    {
                        new newPassword(clickRow.Cells["user_name"].Value.ToString(), userManegeService.ResetPassword(userId)).Show();
                    }
                    else
                    {
                        MessageBox.Show("重置失败!");
                    }
                }

            }
        }
        //重置
        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox2.Text = textBox3.Text = string.Empty;
            comboBox2.SelectedIndex = -1;
            checkBox1.Checked = checkBox2.Checked = false;
            GetAllUser();
        }
        //搜索
        private void button1_Click(object sender, EventArgs e)
        {
            string userName = textBox1.Text;
            int phone = textBox2.Text != "" ? int.Parse(textBox2.Text) : 99;
            long id = textBox3.Text != "" ? long.Parse(textBox3.Text) : 99;
            int sex =comboBox2.SelectedIndex!=-1?(comboBox2.SelectedItem.ToString() == "男" ? 0 : 1):99;
            int authority = 99;
            if (checkBox1.Checked)
            {
                authority = 1;
            }
            else if (checkBox2.Checked)
            {
                authority = 0;
            }
            dataGridView1.DataSource = userManegeService.Search(userName, phone, id, sex, authority);
        }
        //如果哪个憨包两个都选的话
        private void checkBox2_Click(object sender, EventArgs e)
        {
            if (checkBox1.Checked && checkBox2.Checked)
            {
                MessageBox.Show("不会有人又男又女，也不会有人既是管理又是普通用户......");
                checkBox2.Checked = false;
            }
        }
        //上一页
        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (pageNum == 1)
            {
                linkLabel1.Enabled = false;
            }
            else
            {
                linkLabel2.Enabled = true;
                --pageNum;
                textBox4.Text = pageNum.ToString();
                GetAllUser();
            }
        }
        //下一页
        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (userManegeService.GetCount() - pageNum * pageSize < 0)
            {
                linkLabel2.Enabled = false;
            }
            else
            {
                linkLabel1.Enabled = true;
                ++pageNum;
                textBox4.Text = pageNum.ToString();
                GetAllUser();
            }
        }
        //跳转
        private void button3_Click(object sender, EventArgs e)
        {
            pageNum = int.Parse(textBox4.Text.Trim());
            GetAllUser();
        }
        //显示行数
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            pageNum = 1;
            pageSize = (comboBox1.SelectedIndex + 1) * 10;
            GetAllUser();
        }
    }
}
